const {
  mysql
} = require('../qcloud')



module.exports = async (ctx) => {
  function award() {
    return ((Math.floor(Math.random() * (150 - 100 + 1) + 100)) / 1000).toFixed(2)
  }
  let {
    sharer_id,
    friend_id,
    userInfo
  } = ctx.request.body;
  // console.log(sharer_id, friend_id, userInfo);
  // 判断是不是新用户
  /*   let count = await mysql('cSessionInfo')
      .where('open_id', friend_id)
      .select('')
   */
  let oldtime = await mysql('cSessionInfo').select('create_time').where('open_id', friend_id);
  let time = oldtime[0].create_time;
  var dt = new Date(time);
  let now = new Date().getTime();
  // console.log(dt.getTime());
  if (now - dt.getTime() > 10 * 1000) {
    // console.log('老用户');
  } else {
    await mysql('redPacket').insert({
      sharer_id: sharer_id,
      friend_id: friend_id,
      award: award(),
      friend_info: userInfo
    });
  }
  // console.log('新用户');
  // 对邀请新用户的用户进行发奖
  // let friend_info = await mysql('cSessionInfo').where('open_id', friend_id)
  /*  await mysql('redPacket').insert({
     sharer_id: sharer_id,
     friend_id: friend_id,
     award: award(),
     friend_info: userInfo
   }); */

}
